package com.project.medical.sys.service.impl;

import com.project.medical.sys.common.utils.PageUtils;
import com.project.medical.sys.common.utils.Query;
import com.project.medical.sys.entity.DoctorEntity;
import com.project.medical.sys.entity.PatientEntity;
import com.project.medical.sys.service.DoctorService;
import com.project.medical.sys.vo.CheckOutVo;
import com.project.medical.sys.vo.PatientVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.project.medical.sys.dao.CheckOutDao;
import com.project.medical.sys.entity.CheckOutEntity;
import com.project.medical.sys.service.CheckOutService;


@Service("checkOutService")
public class CheckOutServiceImpl extends ServiceImpl<CheckOutDao, CheckOutEntity> implements CheckOutService {
    @Autowired
    DoctorService doctorService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<CheckOutEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)){
            wrapper.eq("id",key).or().like("name",key);
        }
        IPage<CheckOutEntity> page = this.page(
                new Query<CheckOutEntity>().getPage(params),
                wrapper
        );
        List<CheckOutEntity> records = page.getRecords();
        PageUtils pageUtils = new PageUtils(page);
        List<CheckOutVo> collect = records.stream().map(checkOutEntity -> {
            CheckOutVo checkOutVo = new CheckOutVo();
            BeanUtils.copyProperties(checkOutEntity,checkOutVo);
            DoctorEntity byId = doctorService.getById(checkOutEntity.getDoctorId());
            if (byId!=null){
                checkOutVo.setDoctorName(byId.getName());
            }

            return checkOutVo;
        }).collect(Collectors.toList());
        pageUtils.setList(collect);

        return pageUtils;
    }

}